<template>
  <div>
    <el-dialog title="选择常用监听器" class="listener" append-to-body  v-model="visible" draggable>
           <vxe-table
            border="inner"
            auto-resize
            resizable
            height="450px"
            :loading="loading"
            size="small"
            ref="listenerTable"
            show-header-overflow
            show-overflow
            row-id="id"
            highlight-hover-row
            :menu-config="{}"
            :print-config="{}"
            :import-config="{}"
            :export-config="{}"
            @sort-change="sortChangeHandle"
            :sort-config="{remote:true}"
            :data="dataList"
            :checkbox-config="{reserve: true,  highlight: true}">
            <vxe-column type="seq" width="40"></vxe-column>
            <vxe-column type="checkbox"  width="40px"></vxe-column>
            <vxe-column  title="名称" field="name" sortable>
              <template #default="scope">
                <el-link  type="primary" :underline="false" v-if="hasPermission('extension:listener:edit')" @click="edit(scope.row.id)">{{scope.row.name}}</el-link>
                <el-link  type="primary" :underline="false" v-else-if="hasPermission('extension:listener:view')"  @click="view(scope.row.id)">{{scope.row.name}}</el-link>
                <span v-else>{{scope.row.name}}</span>
              </template>
            </vxe-column>
            <vxe-column  title="监听器类型" field="listenerType" sortable> 
              <template #default="scope">
                  {{ scope.row.listenerType === '1'?'执行监听器':'任务监听器'}}
              </template>
            </vxe-column>
            <vxe-column  title="事件" field="event" sortable> </vxe-column>
            <vxe-column  title="值类型" field="valueType" sortable>
               <template #default="scope">
                  {{ {'1':'类', '2':'表达式', '3':'委托表达式'}[scope.row.valueType]}}
              </template>
            </vxe-column>
            <vxe-column  title="值" field="value" sortable> </vxe-column>
          </vxe-table>
      <vxe-pager
        background
        size="small"
        :current-page="tablePage.currentPage"
        :page-size="tablePage.pageSize"
        :total="tablePage.total"
        :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
        :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
        @page-change="currentChangeHandle">
      </vxe-pager>
     <template #footer>
        <span class="dialog-footer">
          <el-button  @click="visible = false">取 消</el-button>
          <el-button  type="primary" @click="submit">确 定</el-button>
        </span>
     </template>
    </el-dialog>
  </div>
</template>

<script>
  import listenerService from '@/api/flowable/listenerService'
  export default {
    data () {
      return {
        searchForm: {
          listenerType: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false,
        visible: false
      }
    },
    activated () {
      this.refreshList()
    },

    methods: {
      init (type) {
        this.searchForm.listenerType = type
        this.visible = true
        this.refreshList()
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        listenerService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then((data) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      submit () {
        const selectReserveRecords = this.$refs.listenerTable.getCheckboxReserveRecords()
        const selectRecords = this.$refs.listenerTable.getCheckboxRecords()
        this.$emit('selectListener', JSON.parse(JSON.stringify(selectReserveRecords.concat(selectRecords))))
        this.visible = false
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
<style>
.listener .el-dialog__body {
    padding: 5px;
}
</style>